package com.example.peachgrove.controller.admin.works;

import com.example.peachgrove.dao.WorkClassMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

@Api(tags = "文章分类管理")
@RestController
@RequestMapping("/workClass")
@ResponseBody
public class WorkClassController {

    @Autowired
    WorkClassMapper workClassMapper;

    @ApiOperation("查询所有分类列表")
    @GetMapping("/getAllWorkClass")
    @ApiImplicitParams({
    })
    public HashMap getAllWorkClass(){
        HashMap res = new HashMap<String,Object>();
        res.put("datas",workClassMapper.getAllWorkClass());
        return res;
    }

    @ApiOperation("创建一个分类")
    @PostMapping("/createWorkClass")
    @ApiImplicitParams({
            @ApiImplicitParam(name="desc",value="描述",dataTypeClass = String.class, paramType = "query"),
            @ApiImplicitParam(name="name",value="类型名",dataTypeClass = String.class, paramType = "query"),
            @ApiImplicitParam(name="coverUrl",value="封面图",dataTypeClass = String.class, paramType = "query")
    })
    public HashMap createWorkClass(HttpServletRequest servletRequest){
        HashMap res = new HashMap();
        int state = workClassMapper.insertWorkClass(
                servletRequest.getParameter("name"),
                servletRequest.getParameter("desc"),
                servletRequest.getParameter("coverUrl"));
        res.put("state",state);
        return res;
    }

    @ApiOperation("修改一个分类")
    @PostMapping("/updateWorkClass")
    @ApiImplicitParams({
            @ApiImplicitParam(name="name",value="类型名",dataTypeClass = String.class, paramType = "query"),
            @ApiImplicitParam(name="desc",value="描述",dataTypeClass = String.class, paramType = "query"),
            @ApiImplicitParam(name="coverUrl",value="封面图",dataTypeClass = String.class, paramType = "query"),
            @ApiImplicitParam(name="workClassId",value="类型id",dataTypeClass = String.class, paramType = "query"),

    })
    public HashMap updateWorkClass(HttpServletRequest servletRequest){
        HashMap res = new HashMap();
        int state = workClassMapper.updateWorkClass(servletRequest.getParameter("name"),
                servletRequest.getParameter("desc"),
                servletRequest.getParameter("coverUrl"),
                servletRequest.getParameter("workClassId"));
        res.put("state",state);
        return res;
    }
    @ApiOperation("删除一个分类")
    @PostMapping("/deleteWorkClass")
    @ApiImplicitParams({
            @ApiImplicitParam(name="workClassId",value="类型id",dataTypeClass = String.class, paramType = "query"),
    })
    public HashMap deleteWorkClass(HttpServletRequest servletRequest){
        HashMap res = new HashMap();
        int state = workClassMapper.deleteByWorkClassId(servletRequest.getParameter("workClassId"));
        res.put("state",state);
        return res;
    }
}
